package com.zhuhjay.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuhjay.commonutils.R;
import com.zhuhjay.eduservice.entity.EduTeacher;
import com.zhuhjay.eduservice.entity.vo.TeacherQuery;
import com.zhuhjay.eduservice.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author ZhuHJay
 * @since 2022-09-06
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")

public class EduTeacherController {

    @Resource
    private EduTeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R queryAll() {
        return R.ok().data("items", teacherService.list(null));
    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    @CacheEvict(value = "TEACHER", allEntries = true)
    public R removeTeacher(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        return teacherService.removeById(id) ? R.ok() : R.error();
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageTeacher(@PathVariable Long current, @PathVariable Long limit) {
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        teacherService.page(teacherPage, null);
        return R.ok()
                .data("total", teacherPage.getTotal())
                .data("rows", teacherPage.getRecords());
    }

    @ApiOperation(value = "条件查询讲师列表")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable Long current, @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        teacherService.page(teacherPage,
                new LambdaQueryWrapper<EduTeacher>()
                        // name 存在 like
                        .like(StringUtils.hasLength(teacherQuery.getName()), EduTeacher::getName, teacherQuery.getName())
                        // level 存在 eq
                        .eq(!ObjectUtils.isEmpty(teacherQuery.getLevel()), EduTeacher::getLevel, teacherQuery.getLevel())
                        // begin 存在 >= ge
                        .ge(StringUtils.hasLength(teacherQuery.getBegin()), EduTeacher::getGmtCreate, teacherQuery.getBegin())
                        // end 存在 <= le
                        .le(StringUtils.hasLength(teacherQuery.getEnd()), EduTeacher::getGmtCreate, teacherQuery.getEnd())
        );
        return R.ok()
                .data("total", teacherPage.getTotal())
                .data("rows", teacherPage.getRecords());
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    @CacheEvict(value = "TEACHER", allEntries = true)
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        return teacherService.save(eduTeacher) ? R.ok() : R.error();
    }

    @ApiOperation(value = "根据ID获取讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        return R.ok().data("teacher", teacherService.getById(id));
    }

    @ApiOperation(value = "修改讲师信息")
    @PostMapping("updateTeacher")
    @CacheEvict(value = "TEACHER", allEntries = true)
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        return teacherService.updateById(eduTeacher) ? R.ok() : R.error();
    }
}

